//
//  CommentController.m
//  MedicineBox
//
//  Created by dllo on 15/9/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CommentController.h"
#import "YAScrollSegmentControl.h"
#import "CommentCollectionCell.h"
#import "AsyncConnection.h"
#import "DKNightVersion.h"
@interface CommentController ()<YAScrollSegmentControlDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) YAScrollSegmentControl *scrollSC;
@property (nonatomic, retain) UILabel *label;

@property (nonatomic, retain) NSDictionary *allDic;
@property (nonatomic, retain) NSDictionary *goodDic;
@property (nonatomic, retain) NSDictionary *commonDic;
@property (nonatomic, retain) NSDictionary *badDic;


@end

@implementation CommentController

- (void)dealloc
{
    
    [_allDic release];
    [_goodDic release];
    [_commonDic release];
    [_badDic release];
    [_collectionV release];
    [_label release];
    [_scrollSC release];
    [super dealloc];
    
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 5, 100, 30)];
    titleLabel.text = @"商品评论";
    titleLabel.nightTextColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:20];
    self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    returnButton.frame = CGRectMake(0, 8, 40, 20);
    [returnButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnButton];
    [self creatSubview:nil count2:nil count3:nil count4:nil];
    
    UICollectionViewFlowLayout *flowLay = [[UICollectionViewFlowLayout alloc] init];
    flowLay.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLay.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 60);
    flowLay.minimumLineSpacing = 0;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60)collectionViewLayout:flowLay];
    _collectionV.backgroundColor = [UIColor whiteColor];
    _collectionV.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.collectionV.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 60);
    self.collectionV.pagingEnabled = YES;
    _collectionV.delegate = self;
    _collectionV.dataSource = self;
    [self.view addSubview:_collectionV];
    [self.collectionV registerClass:[CommentCollectionCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [_collectionV release];
    [flowLay release];
    
    [self webConnection];
    
}

- (void)webConnection
{

    NSString *webSite = [NSString stringWithFormat:@"http://mobi.111.com.cn/ApiControl?method=product.review&pId=%@&category=ALL&pageId=1&pageSize=2147483647&encryptversion=2&sign=30043B750BEB08BDA78226E773BBB770&timestamp=20150926123919&os=android&venderId=2011102716210000&signmethod=md5&format=json&type=mobile&channelName=huawei&versionName=4.6.1&versionCode=477&screensize=480*854&width=480&height=854",_idStr];
    
    [AsyncConnection asyncConnectionWithUrl:webSite name:nil parameters:nil block:^(id data) {
      
        self.allDic = [[data objectForKey:@"data"] objectForKey:@"review"];
      
        [self.collectionV reloadData];
        
        [self creatSubview: [_allDic objectForKey:@"allCount"] count2:[_allDic objectForKey:@"goodCount"] count3:[_allDic objectForKey:@"middleCount"] count4:[_allDic objectForKey:@"poorCount"]];
        
        
    } get:@"post"];
    
    NSString *googWeb = [NSString stringWithFormat:@"http://mobi.111.com.cn/ApiControl?method=product.review&pId=%@&category=GOOD&pageId=1&pageSize=2147483647&encryptversion=2&sign=1E23B0EE49C54654B3675C200DD2BB1C&timestamp=20150926124158&os=android&venderId=2011102716210000&signmethod=md5&format=json&type=mobile&channelName=huawei&versionName=4.6.1&versionCode=477&screensize=480*854&width=480&height=854",_idStr];
    [AsyncConnection asyncConnectionWithUrl:googWeb name:nil parameters:nil block:^(id data) {
        
       self.goodDic = [[data objectForKey:@"data"] objectForKey:@"review"];
        [self.collectionV reloadData];

        
        
    } get:@"post"];
    
    
    NSString *commonWeb = [NSString stringWithFormat:@"http://mobi.111.com.cn/ApiControl?method=product.review&pId=%@&category=MIDDLE&pageId=1&pageSize=2147483647&encryptversion=2&sign=36BA9D9847949FBE9FE2C53906D80837&timestamp=20150926124551&os=android&venderId=2011102716210000&signmethod=md5&format=json&type=mobile&channelName=huawei&versionName=4.6.1&versionCode=477&screensize=480*854&width=480&height=854",_idStr];
    [AsyncConnection asyncConnectionWithUrl:commonWeb name:nil parameters:nil block:^(id data) {
        
       self.commonDic = [[data objectForKey:@"data"] objectForKey:@"review"];
        [self.collectionV reloadData];
        
    } get:@"post"];
    
    NSString *badWeb = [NSString stringWithFormat:@"http://mobi.111.com.cn/ApiControl?method=product.review&pId=%@&category=POOR&pageId=1&pageSize=2147483647&encryptversion=2&sign=7FC8A0ED9EE000F3296EFDF4BFC56475&timestamp=20150926124649&os=android&venderId=2011102716210000&signmethod=md5&format=json&type=mobile&channelName=huawei&versionName=4.6.1&versionCode=477&screensize=480*854&width=480&height=854",_idStr];
    [AsyncConnection asyncConnectionWithUrl:badWeb name:nil parameters:nil block:^(id data) {
        
       self.badDic = [[data objectForKey:@"data"] objectForKey:@"review"];
        [self.collectionV reloadData];
        
    } get:@"post"];
    

}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CommentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];

    cell.type = @"comment";
    cell.nightBackgroundColor = UIColorFromRGB(0x343434);
    if (indexPath.item == 0 && _allDic != nil) {
        
        [cell getArray:_allDic str:@"ALL" idstr:_idStr];
        
    } else if (indexPath.item == 1 && _goodDic != nil) {
    
        [cell getArray:_goodDic str:@"GOOD" idstr:_idStr];
    
    } else if (indexPath.item == 2 && _commonDic != nil) {
    
        [cell getArray:_commonDic str:@"MIDDLE" idstr:_idStr];
    
    } else if (indexPath.item == 3 && _badDic != nil) {
    
        [cell getArray:_badDic str:@"POOR" idstr:_idStr];
    
    }
    cell.nightBackgroundColor = UIColorFromRGB(0x343434);
    return cell;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return 4;

}

- (void)creatSubview:(NSString *)allCount count2:(NSString *)godCount count3:(NSString *)commonCount count4:(NSString *)poorCount
{

    NSArray *array = [NSArray arrayWithObjects:allCount,godCount,commonCount,poorCount, nil];
    if (allCount == nil) {
        
        self.scrollSC = [[YAScrollSegmentControl alloc] initWithFrame:CGRectMake(20, -10, self.view.frame.size.width - 40 , 60)];
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height - 5, 36, 3)];
        _label.backgroundColor = [UIColor colorWithRed:0.6 green:0.7 blue:0.6 alpha:1];
        [self.scrollSC addSubview:_label];
        for (int i = 0; i < 4; i++) {
            
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
            label.tag = 250 + i;
            label.font = [UIFont systemFontOfSize:10];
            label.textAlignment = NSTextAlignmentCenter;
            if (self.view.frame.size.width == 375) {
                
                label.frame = CGRectMake(32 + (self.view.frame.size.width - 110 ) / 4 * i , self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height - 5, (self.view.frame.size.width - 110) / 4, 20);
                
            } else if (self.view.frame.size.width < 375) {
                
                label.frame = CGRectMake(28 + (self.view.frame.size.width - 100 ) / 4 * i , self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height - 5, (self.view.frame.size.width - 100) / 4, 20);
                
            } else if (self.view.frame.size.width > 375) {
                
                label.frame = CGRectMake(32 + (self.view.frame.size.width - 185 ) / 4 * i , self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height - 20, (self.view.frame.size.width - 185) / 4, 20);
                
            }
            
            [_scrollSC addSubview:label];
            label.text = [NSString stringWithFormat:@"(0)"];
            [label release];
            
        }

        if (self.view.frame.size.width == 375) {
            
            self.scrollSC.edgeMargin = 15;
            self.label.frame = CGRectMake(48, self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 3);
            
        } else if (self.view.frame.size.width < 375) {
            
            self.scrollSC.edgeMargin = 10;
            self.label.frame = CGRectMake(35, self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 3);
            
        } else if (self.view.frame.size.width > 375) {
            
            _scrollSC.frame = CGRectMake(60, 0, self.view.frame.size.width - 120, 40);
            self.scrollSC.edgeMargin = 10;
            self.label.frame = CGRectMake(45, self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 3);
            
        }
        
        
        _scrollSC.delegate = self;
        self.scrollSC.buttons = [NSArray arrayWithObjects:@"全部",@"好评",@"中评",@"差评", nil];
        self.scrollSC.selectedIndex = 0;
        [_scrollSC setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [self.view addSubview:_scrollSC];
        [_scrollSC release];
        
        
    } else if (allCount != nil) {
    
        for (int i = 0; i < 4; i++) {
            
            UILabel *label = (UILabel *)[self.view viewWithTag:250 + i];
            label.text = [NSString stringWithFormat:@"(%@)",[array objectAtIndex:i]];
        
        }
        
    }

}


- (void)didSelectItemAtIndex:(NSInteger)index
{

    
    NSString *buttonName = [self.scrollSC.buttons objectAtIndex:index];
    
    if ([buttonName isEqualToString:@"全部"]) {
        
        self.collectionV.contentOffset = CGPointMake(0, 0);
        NSLog(@"全部");
        
    } else if ([buttonName isEqualToString:@"好评"]) {
        
        
        self.collectionV.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        NSLog(@"好评");
        
    } else if ([buttonName isEqualToString:@"中评"]) {
        
        self.collectionV.contentOffset = CGPointMake(self.view.frame.size.width * 2, 0);
        NSLog(@"中评");
        
    } else if ([buttonName isEqualToString:@"差评"]) {
        
        self.collectionV.contentOffset = CGPointMake(self.view.frame.size.width * 3, 0);
        NSLog(@"差评");
        
    }


}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    if (self.view.frame.size.width == 375) {
        
        self.label.frame = CGRectMake(48 + (scrollView.contentOffset.x / self.view.frame.size.width) * (30 + 36), self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 2);
        
    } else if (self.view.frame.size.width < 375) {
        
        self.label.frame = CGRectMake(40 + (scrollView.contentOffset.x / self.view.frame.size.width) * (20+ 36), self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 2);
        
    } else if (self.view.frame.size.width > 375) {
        
        self.label.frame = CGRectMake(45 + (scrollView.contentOffset.x / self.view.frame.size.width) * (20+ 36), self.scrollSC.frame.origin.y + self.scrollSC.frame.size.height + 15, 36, 2);
        
    }
    
    
}



- (void)backButtonAction:(id)sender
{

    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
